<?php
namespace app\admin\service;
use Curl\Curl;
use think\Exception;

class CallCenter
{
    //呼叫中心接口url
    protected $ccApiUrl = null;
    protected $curl = null;
    protected $errJson = '{"result":"error","code":500,"msg":"接口调用失败！"}';

    public function __construct(Curl $curl)
    {
        $this->ccApiUrl = trim(\Config::get('cc.cc_api_url'), '/');
        if(0 && \Env::get('app_debug') ){   //是否开启代理调试
            $curl->setOpt(CURLOPT_PROXY, "127.0.0.1");
            $curl->setOpt(CURLOPT_PROXYPORT, 8888);
        }
        $this->curl = $curl;
    }

    //获取坐席登录的初始化状态(分机hint状态和DND状态)
    public function getExtenInitState($exten){
        //$initState = ['hintState'=>'', 'dndState'=>'',];
        $initState = json_decode(json_encode(['hintState'=>'', 'dndState'=>'',]));  //前台模板要求是对象
        try{
            $retApi = $this->curl->get($this->ccApiUrl .'/index.php?m=Api&c=Exten&a=initState', array(
                'exten' => $exten,
            ));

            if($retApi && $retApi->code==200){
                $initState = $retApi->data;
            }
            return $initState;

        }catch (Exception $e){
            //log('呼叫中心接口错误：获取分机初始状态');
        }
    }

    //点击拨号
    public function clickcall($exten, $phone){
        try{
            $retApi = $this->curl->post($this->ccApiUrl .'/index.php?m=Api&c=Exten&a=clickcallout', array(
                'dailtech' => 'SIP/'.$exten,
                'src' => $exten,
                'dst' => $phone,
            ));
            return $retApi;

        }catch (Exception $e){
            return json_decode($this->errJson);

        }
    }

    //转接接口
    public function transfer($exten, $phone){
        try{
            $retApi = $this->curl->post($this->ccApiUrl .'/index.php?m=Api&c=Exten&a=transfer', array(
                'exten' => $exten,
                'phone' => $phone,
            ));
            return $retApi;

        }catch (Exception $e){
            return json_decode($this->errJson);

        }
    }

    //多方通话接口
    public function multicall($exten, $phone){
        try{
            $retApi = $this->curl->post($this->ccApiUrl .'/index.php?m=Api&c=Exten&a=multicall', array(
                'exten' => $exten,
                'phone' => $phone,
            ));
            return $retApi;

        }catch (Exception $e){
            return json_decode($this->errJson);

        }
    }

    //模拟来电弹屏
    public function popscreen($exten, $phone){
        try{
            $pubData = array(
                'type' => 1,    //呼入
                'data' => [],
            );
            $pubData['data'] = [
                'caller' => $phone,
                'callee' => $exten,
                'duserid' => 0, //随便写一个
                'uniqueid' => "1517152140.94",
                'QueueNum' => "",
                'TransferFrom' => "",
            ];

            $this->curl->setHeader('Content-Type', 'application/json');
            $retApi = $this->curl->post($this->ccApiUrl .'/da/pub?daID='.$exten .'&_=' . mt_rand(), $pubData);
            return $retApi;

        }catch (Exception $e){
            return json_decode($this->errJson);

        }
    }

}